package avicit.bdp.dcs.iotdb;

import avicit.bdp.common.utils.database.IotDbSessionUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.iotdb.isession.pool.SessionDataSetWrapper;
import org.apache.iotdb.tsfile.read.common.Field;
import org.apache.iotdb.tsfile.read.common.RowRecord;

import java.util.List;

/**
 * @author liyb
 */
public class Test {

    public static void main(String[] args) {
        try {
            IotDbSessionUtils.createSessionPool("192.168.0.24", 6667,
                    "root", "root", 50);

//            IotDbSessionUtils.deleteTimeseries("root.liyb.test.name");
//            IotDbSessionUtils.deleteTimeseries("root.liyb.test.age");
//            IotDbSessionUtils.deleteTimeseries("root.liyb.test.work");

            //IotDbSessionUtils.deleteStorageGroup("root.liyb");

//            IotDbSessionUtils.createTimeseries("root.liyb.test.name",
//                    TSDataType.TEXT, TSEncoding.PLAIN, CompressionType.SNAPPY);
//
//            IotDbSessionUtils.createTimeseries("root.liyb.test.age",
//                    TSDataType.INT32, TSEncoding.RLE, CompressionType.SNAPPY);
//
//            IotDbSessionUtils.createTimeseries("root.liyb.test.work",
//                    TSDataType.TEXT, TSEncoding.PLAIN, CompressionType.SNAPPY);

            System.out.println("123");
            System.out.println(IotDbSessionUtils.checkTimeseriesExists("root.liyb.test"));

            SessionDataSetWrapper sessionDataSetWrapper =
                    IotDbSessionUtils.executeQueryStatement("select count(*) from root.liyb.test where time>=1615876833000 and time<=1615876843000");
            System.out.println(sessionDataSetWrapper.getColumnNames());

            long x = 0;
            long y = 0;
            while (sessionDataSetWrapper.hasNext()) {
                RowRecord rowRecord = sessionDataSetWrapper.next();
                List<Field> fieldList = rowRecord.getFields();
                if (CollectionUtils.isNotEmpty(fieldList)) {
                    for(Field field : fieldList){
                        y = field.getLongV();
                        if(x < y){
                            x = y;
                        }
                    }
                }
                System.out.println("num=" + x);
            }

            //SessionDataSetWrapper sessionDataSetWrapper =
            //        IotDbSessionUtils.pool.executeQueryStatement("show timeseries root.liyb.test");
            //System.out.println(sessionDataSetWrapper.getColumnNames());
            //
            //while (sessionDataSetWrapper.hasNext()) {
            //    RowRecord rowRecord = sessionDataSetWrapper.next();
            //    List<Field> list = rowRecord.getFields();
            //    String field = list.get(0).toString();
            //    field = field.substring(field.lastIndexOf(".") + 1);
            //    System.out.println(field + "  " + list.get(3).toString());
            //}
            //
            //SessionDataSetWrapper sessionDataSetWrapper2 =
            //        IotDbSessionUtils.pool.executeQueryStatement("show devices");
            //System.out.println(sessionDataSetWrapper.getColumnNames());
            //while (sessionDataSetWrapper2.hasNext()) {
            //    RowRecord rowRecord = sessionDataSetWrapper2.next();
            //    List<Field> list = rowRecord.getFields();
            //    String field = list.get(0).toString();
            //    System.out.println(field);
            //}

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
